<template>
  <div class="search">
    <el-autocomplete
      clearable
      :fetch-suggestions="querySearch"
      placeholder="请输入医院名称"
      class="form"
      :trigger-on-focus="false"
      v-model="hosname"
      @select="goDetail"
    />

    <el-button  type="primary" plain :icon="Search">搜索</el-button>
  </div>
</template>

<script setup lang="ts">
import { Search } from "@element-plus/icons-vue";
import {ref} from "vue";
import {reqHospitalInfo} from "@/api/home";
import {HospitalInfo} from "@/api/home/type";
import {useRouter} from "vue-router";
// 搜索关键字
let hosname = ref<string>('')
let $router = useRouter()

/**
 * 顶部组件的回调
 * @param keyword 关键词
 * @param cb 回调函数
 */
const querySearch = async (keyword: string, cb: any) => {
  let result: HospitalInfo = await reqHospitalInfo(keyword)
  let showData = result.data.map(item => {
    return {
      value: item.hosname,
      hoscode: item.hoscode
    }
  })
  cb(showData)
}

/**
 * 点击某一个推荐项,跳转到医院详情
 * @param item
 */
const goDetail = (item) => {
  $router.push({
    query: item.hoscode,
    path: '/hospital'
  })
}
</script>

<style scoped lang="scss">
.search {
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 10px 0;
  ::v-deep(.el-input__wrapper) {
    width: 600px;
    margin-right: 10px;
  }

}
</style>
